/**
  代理对象访问目标对象
    代理Proxy
        目标对象
        处理程序对象
        代理对象
 */
function test1() {
    // 目标对象
	const obj = {
		name: 'jack',
		age: 18,
	}
    // 处理程序对象
	const handler = {}
    // 代理对象
	const proxy = new Proxy(obj, handler)
    
	//代理访问对象
	console.log('obj.name :', obj.name, ' proxy.name :', proxy.name)
    //obj.name : jack  proxy.name : jack
    //代理改变对象
    proxy.name = 'rose'
    console.log('obj.name :', obj.name, ' proxy.name :', proxy.name)
    //obj.name : rose  proxy.name : rose
}

/**
 * 访问代理对象属性，会执行处理程序的get方法,get方法决定是否返回内容
 */
function test3() {
	const obj = {
		name: 'jack',
		age: 18,
	}
	const handler = {
		get: function (target, property, receiver) {
			console.log('get >>> target :', target, '  property  :', target[property], '  receiver :', receiver)
		},
	}

	const proxy = new Proxy(obj, handler)

	// console.log('obj.name ', obj.name)
	console.log('proxy.name ', proxy.name)
}

function test4() {
	const obj = {
		name: 'jack',
		age: 18,
	}
	const handler = {
		get: function (target, proper, receiver) {
			return target[proper]
		},
	}

	const proxy = new Proxy(obj, handler)

	// console.log('obj.name ', obj.name)
	console.log('proxy.name ', proxy.name)
	console.log('proxy.age', proxy.age)
}

function test5() {
	const obj = {
		name: 'jack',
		age: 18,
	}
	const handler = {
		get: Reflect.get, //Gets the property of target, equivalent to target[propertyKey] when receiver === target
		set: Reflect.set, //Sets the property of target, equivalent to target[propertyKey] = value when receiver === target.
	}

	const proxy = new Proxy(obj, handler)

	// console.log('obj.name ', obj.name)
	console.log('proxy.name ', proxy.name)
	console.log('proxy.age', proxy.age)
	proxy.name = 'rose'
	console.log('proxy.name ', proxy.name)
}
test1()
